#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int main()
{
    float height,width;
	width=600.0;
	height=400.0;
	initgraph(width,height);//定义框子

	float ball_x,ball_y,r;
	ball_x=width/6;
	r=10;
	ball_y=height/2;//定义小球的数据

	float vy,a;
	vy=0;
	a=0.4;//定义小球速度和加速度

	float rect_left,rect_right,rect_height,rect_width;
	rect_left=580;
	rect_height=100;
	rect_width=20;
	rect_right=rect_left+rect_width;//定义长方形白块

	float rect_vx;
	rect_vx=-7;//定义长方形速度

	int score=0;//定义分数
    while(1)
	{
		if(kbhit())
		{
			char c=_getch();
			if(c==' ')
		    {
		        vy=-8;
		    };
		}//让球起跳

		vy=vy+a;
		ball_y=ball_y+vy;//规划运动方式

		if(ball_y>height-r)
		{
			vy=0;
			ball_y=height-r;//落地停止，不在下落
		}

		rect_left=rect_left+rect_vx;
		if(rect_left<=0)
		{
			score=score+1;
			rect_left=width;//到头方块重头再来
			rect_height=rand()%250+50;
			rect_vx=-rand()%5-5;//随机高度和速度
		}
        if((ball_y+r>=height-rect_height)&&(ball_x+r>=rect_left)&&(ball_x-r<=rect_left+rect_width))
		{
			score=-1;
			Sleep(100);
		}//触碰机制1

		if((ball_y-r<=height-rect_height-100)&&(ball_x+r>=rect_left)&&(ball_x-r<=rect_left+rect_width))
		{
			score=-1;
			Sleep(100);
		}//触碰机制2

		if(ball_y+r>=400)
		{
			ball_y=400-r;
			score=-1;
		}//触碰机制3
		
		if(ball_y-r<=0)
		{
			ball_y=r;
			score=-1;
		}//触碰机制4
        cleardevice();//清空原图案
		fillrectangle(rect_left,height-rect_height,rect_left+rect_width,height);//绘制长方形边框10
		fillrectangle(rect_left,0,rect_left+rect_width,height-rect_height-100);//绘制长方形边框2
		fillcircle(ball_x,ball_y,r);//绘制小球

		TCHAR s[20];
		_stprintf(s,_T("%d"), score);
		settextstyle(40, 0,_T("宋体"));
		outtextxy(50,30,s);
	_getch();
	closegraph();
	return 0;
}